package com.dly.hope.dao;

import com.dly.hope.model.ro.GetUserShowRo;
import com.dly.hope.model.vo.ShowVo;
import com.dly.hope.modle.QShowContent;
import com.dly.hope.pageUtils.Page;
import com.dly.hope.pageUtils.PageRo;
import com.querydsl.core.types.Projections;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ShowContentDao extends BaseDao{

    public QShowContent basePath(){
        return QShowContent.showContent;
    }

    public Page<ShowVo> getAllPage(PageRo pageRo, List<String> friendIds){
        Page<ShowVo> showVoPage = new Page<>(pageRo);
        List<ShowVo> fetch = queryFactory
                .select(Projections.bean(
                        ShowVo.class,
                        basePath().id.as("id"),
                        basePath().createUser.as("userId"),
                        basePath().text.as("text"),
                        basePath().createDate.as("createDate")
                ))
                .from(basePath())
                .limit(pageRo.getPageSize())
                .offset((long) pageRo.getPageSize() * (pageRo.getPageNumber()-1))
                .orderBy(basePath().createDate.desc())
                .where(efficient(basePath().dataStatus).and(basePath().createUser.in(friendIds)))
                .fetch();
        showVoPage.setContent(fetch);
        return showVoPage;
    }

    public Page<ShowVo> getUserPage(GetUserShowRo getUserShowRo){
        Page<ShowVo> showVoPage = new Page<>(getUserShowRo);
        List<ShowVo> fetch = queryFactory
                .select(Projections.bean(
                        ShowVo.class,
                        basePath().id.as("id"),
                        basePath().createUser.as("userId"),
                        basePath().text.as("text"),
                        basePath().createDate.as("createDate")
                ))
                .from(basePath())
                .limit(getUserShowRo.getPageSize())
                .offset((long) getUserShowRo.getPageSize() * (getUserShowRo.getPageNumber()-1))
                .orderBy(basePath().createDate.desc())
                .where(efficient(basePath().dataStatus)
                        .and(basePath().createUser.eq(getUserShowRo.getUserId())))
                .fetch();
        showVoPage.setContent(fetch);
        return showVoPage;
    }
}
